/** 医生详情*/

$(function() {
	
	var isInit;
	var $pageNo = 1;
	var $pageSize = 5;
	if(seachType==2){
	   $pageSize=10;	
	}
	////初始化列表页面
	$(window).on("load", function(){isInit=true; queryDoctorList();});
	//点击展现弹窗
	$(".choice-doctorList").on("click",function(){
		$(".global-pop1").css("display",'block');
		$(".popBox .bot2").on("click",function(){
			location.replace("rapidly-phone.html");
		});
	});
	//搜索事件
	/*$('.searchBtn').on("touchstart", function(){isInit=false;searchDoctorList();});*/
	
	//初始化MUI事件
	mui.init({
		pullRefresh: {
			container: '#pullrefresh',
			down: {
				callback: pulldownRefresh
			}
		}
	});

	//判断是否为刷新请求
    var isRefresh=false;
	function pulldownRefresh() {
 		isRefresh=true;
 		var totalCount=$(".mui-table-view").length;
 		if(totalCount<$pageSize){
 		   return isEmptyList();	//如果当前页筛选的条数小于1页，则不请求
 		}else{
 		  var returnData = loadPageData($pageNo, $pageSize);
		  if(returnData && returnData.list != undefined) { 
			 renderBody(returnData.list);
			 $pageNo++;
		  } else {
			 return isEmptyList();
		    } 
 	    }	
	}
	
	function isEmptyList(){
		 mui.toast('没有更多的数据');
		 mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		 return false;
	}

	/**
	 * 调用Model 检索接口
	 * @param {Object} _pageNo
	 * @param {Object} _pageSize
	 */
	function loadPageData(_pageNo, _pageSize) {
		var doctorObj = {};
		
		if(isInit){
	        var $keyword=utilModule.getQueryString("keyword");//从推荐页面传入
		    if($keyword!=null){
	   	        $keyword=unescape($keyword);
	   	        $("#keyword").val($keyword);
	  		 }	
		  	var diseaseId=utilModule.getQueryString("diseaseId");
		  	if(diseaseId!=null){
		  		$("#diseaseId").val(diseaseId)
		  	}
		  		 
		}else{
			var diseaseId = $("#diseaseId").val();
		}
		var realname = $("#keyword").val(); 
		var params = {
			pageSize: _pageSize,
			pageNo: _pageNo,
			realname: realname,
			diseaseId: diseaseId,
			skilful:realname,
			hospitalName:realname
				
		};
		doctorModule.queryList(params, function(callBack) {
			console.log(callBack);
			results = callBack;
			if(results.diseaseList != null && seachType == 1) {
				initDiseaseList(results.diseaseList); //处理症状数据
			}
			doctorObj.list = results.doctorList; //信息
		});
		return doctorObj;
	}

	/* //渲染数据
	 * @param {Object} data
	 * 
	 */
	function renderBody(data) {
		setTimeout(function() {
			//body 数据面板
			var table =document.body.querySelector(".mui-scroll") ;
			
			if(data==null || data.length<1){
		   	  $('.mui-scroll').html("<div align='center'>没有符合条件的数据</div>");
		   	  mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		   	  return false;
			}
			
			//当前的body数据的行数
			var cells = document.body.querySelectorAll('.mui-table-view');
			
			var keyWord=$("#keyword").val();
			
			if(!isRefresh || keyWord!=""){
		       table.innerHTML="";
			}
			
			for(var i = 0, leng = data.length; i < leng; i++) {
				var object = data[i];
				if(typeof(object.imprice) =="undefined"){
					imprice = "未开启";
				}else{
					var imprice = object.imprice;
					if(imprice == null || imprice == 0) {
						imprice = "免费";
					} else {
						imprice = imprice.toFixed(2);
					}
				}
				var $doctortitle = object.doctortitle;
				if(utilModule.checkUndefined($doctortitle)=="") {
					$doctortitle = " ";
				} 
				var $avatar = object.avatar;
			/*	var deptName=object.deptName==null?"神经内科":object.deptName;*/
				var div = document.createElement('li');
				div.className = "mui-table-view systemdoctor";
				var resultHtml = new Array();
			    resultHtml.push("<a class='goto-doctor' href=\"#\" v=\"" + object.id + "\">");
			    if($avatar){
			    	resultHtml.push("<div class=\"picture logo\" ><img src=\"" + $avatar + "\"/></div>");	
			    }else{	
			    	resultHtml.push("<div class=\"picture logo\" ><img src='../../images/default-doctor.png'/></div>");
			    }
				resultHtml.push("<p class='name'>"+object.realname+"<b class='font2 doctortitle'>"+$doctortitle+"</b></p>");
					doctortitle = " ";
				resultHtml.push("<div class=\"star\"><span class=\"active\"></span><span class=\"active\"></span><span class=\"active\"></span><span class=\"active\"></span><span></span></div>")
				resultHtml.push("<p class=\"school\">" + object.hospitalName + "</p>")
				resultHtml.push("<p class=\"info\" style=\"line-height: 0.3rem;\">")
				var skilfulList=object.diseasesList;
				var skill ="";
					if(typeof(data[i].diseasesList[0].disname)!='undefined'){
						for(var j = 0, len = data[i].diseasesList.length; j < len; j++){
							if(j==len-1){
								skill+=data[i].diseasesList[j].disname;	
							}else{
								skill+=data[i].diseasesList[j].disname+"、"
							}
						}
					}else{
						skill="";
					}
				resultHtml.push("擅长：" + skill + "</p></a>")
				resultHtml.push("<p class=\"choice\">")
				resultHtml.push("<font class=\"doct-left\">咨询量<span class=\"consult-num\">"+object.servicetimes+"</span></font>");
				resultHtml.push("<font class=\"doct-right\"><span class=\"consult-imprice\">"+(object.imprice)*10+"</span>" + "元/10分钟</font>");
				resultHtml.push("</p></li>")
				div.innerHTML = resultHtml.join("");
				//将查询数据插入到最上层
				 table.insertBefore(div, table.firstChild);
			}
		}, 1000);
	}
	
	//页面初始化及检索
	function queryDoctorList() {
		var returnData = loadPageData(1, $pageSize);
		if(seachType == 1) { 
			//直接模板渲染
			for(var i = 0, leng = returnData.list.length; i < leng; i++){
				//返回disname不存在,就没有undefined
				if(!returnData.list[i].disname){
					disname = " ";
				}else{
					// 循环disname
					for(var j = 0 , len = returnData.list[i].diseasesList.length ; j < len; j++){
					
						if(j==returnData.list[i].diseasesList.length-1){
								returnData.list[i].disname+=returnData.list[i].diseasesList[j].disname;	
						}else{
							returnData.list[i].disname+=returnData.list[i].diseasesList[j].disname+"、";	
						}	
					}
					// 去掉重复的disname(第一个)
					var n = returnData.list[i].diseasesList[0].disname,m=returnData.list[i].disname;
				 	m=m.substring(n.length);
					returnData.list[i].disname=m;
				}
				
			}
			utilModule.renderHtml("#doctorDataBody", "doctorDataDiv", returnData);
		} else {
			renderBody(returnData.list); //列表界面滚动效果
		}
	}
	
});
